package com.solr.self.index;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;

public class ReaderIndex {

	public  CommonsHttpSolrServer getServer(String hostUrl){
	       CommonsHttpSolrServer server = null;
	       try {
	           server = new CommonsHttpSolrServer(hostUrl);
	           server.setConnectionTimeout(100);
	           server.setDefaultMaxConnectionsPerHost(100);
	           server.setMaxTotalConnections(100);
	       } catch (IOException e) {
	           System.out.println("请检查tomcat服务器或端口是否开启!");
	       }
	       return server;
	    }
	    public void indexDocuements(String path,String[] hostUrls) throws CorruptIndexException, LockObtainFailedException, IOException{
	       File pareFile = new File(path);
	       List<String> list = new ArrayList<String>();
	       getFile(pareFile,list); //递归方法得到路径保存到list中
	       System.out.println("***程序一共递归到"+list.size()+"个索引目录***");
	       int arevageSize = list.size()/hostUrls.length;// 根据主机数平分目录
	       int remainSize  = list.size()%hostUrls.length;//取余
	       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       for(int i=0;i<hostUrls.length;i++){
	           Date startDate = new Date();
	           String url = hostUrls[i].substring(hostUrls[i].indexOf("//")+2,hostUrls[i].lastIndexOf("/"));
	           System.out.println("第"+(i+1)+"次，在主机:"+url+" 上创建索引,创建时间  "+sdf.format(startDate));
	           if(i==(hostUrls.length-1)){
	              list(list,arevageSize*i,arevageSize*(i+1)+remainSize,hostUrls[i]);
	           }else{
	              list(list,arevageSize*i,arevageSize*(i+1),hostUrls[i]);
	           }
	           Date endDate = new Date();
	           System.out.println("本次索引结束时间为:"+sdf.format(endDate));
	       }
	 
	    }
	    public void list(List<String> list,int start,int end,String url){
	       CommonsHttpSolrServer server = getServer(url);
	       for(int j=start;j<end;j++){
	           try {
	              long startMs = System.currentTimeMillis();
	              hostCreate(list.get(j),server);
	              long endMs = System.currentTimeMillis();
	              System.out.println("程序第"+(j+1)+"个目录处理完毕,目录路径:"+list.get(j)+", 耗时"+(endMs-startMs)+"ms");
	           } catch (CorruptIndexException e) {
	              e.printStackTrace();
	           } catch (IOException e) {
	              e.printStackTrace();
	           }
	       }
	    }
	    public void getFile(File fileDirectory,List<String> list){
	       if(fileDirectory.isDirectory()){
	           File[] files = fileDirectory.listFiles();
	           for(File file :files){
	           getFile(file,list);
	           }
	       }else if(fileDirectory.isFile()){
	           String filePath = fileDirectory.getPath();
	           String path = filePath.replace('\\', '/');
	           if(path.endsWith(".cfs")){
	              int lastIndex = path.lastIndexOf("/");
	              String directory = path.substring(0,lastIndex);
	              list.add(directory);
	           }
	       }
	    }
	    @SuppressWarnings("deprecation")
	    public void hostCreate(String directory,CommonsHttpSolrServer server) throws CorruptIndexException, IOException{
	    	 
	       IndexReader reader = IndexReader.open(FSDirectory.open(new File(directory)));
	       List<BookIndex2> beans = new ArrayList<BookIndex2>();
	       for(int i=0;i<reader.numDocs();i++){
	           Document doc = reader.document(i);
	           BookIndex2 book = new BookIndex2();
	           book.setZjid(doc.getField("zjid").stringValue());
	           book.setAddress(doc.getField("address").stringValue());
	           book.setAuthor(doc.getField("author").stringValue());
	           book.setBookname(doc.getField("bookname").stringValue());
	           book.setDxid(doc.getField("dxid").stringValue());
	           book.setFenlei(doc.getField("fenlei").stringValue());
	           book.setFenleiurl(doc.getField("fenleiurl").stringValue());
	           book.setMulu(doc.getField("mulu").stringValue());
	           book.setPublishDate(doc.getField("publishDate").stringValue());
	           book.setQwpos(doc.getField("qwpos").stringValue());
	           book.setSsid(doc.getField("ssid").stringValue());
	           book.setZhangjie(doc.getField("zhangjie").stringValue());
	           beans.add(book);
	           }
	       createIndex(beans,server);
	       beans.clear();
	       reader.close();
	    }
	    public void createIndex(List<BookIndex2> beans,CommonsHttpSolrServer server){
	       try {
	              server.addBeans(beans);
	              server.commit();
//	            server.optimize();
	       } catch (SolrServerException e) {
	           e.printStackTrace();
	       } catch (IOException e) {
	           e.printStackTrace();
	       }
	    }
	   
	    public static void main(String[] args) throws CorruptIndexException, IOException{
	       ReaderIndex reader = new ReaderIndex();
	       String path = "F:\\index\\newsinfo";
//	       String[] hosts = new String[] {"http://172.19.4.5:7080/solrTest"};
	       String[] hosts = new String[] {"http://192.168.169.121:9888/solr","http://192.168.169.121:9088/solr","http://192.168.169.48:9888/solr"};
	      
	       long startTime = new Date().getTime();
	       reader.indexDocuements(path,hosts);
	       long endTime = new Date().getTime();
	       System.out.println("-------------------");
	       long ms = (endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;
	       System.out.println("全部文档索引完毕，一共用了"+(endTime-startTime)/60000+"分" +
	              ""+((endTime-startTime)%60000)/1000+"秒"+ms+"毫秒");
	      
	    }
	
}
